package com.ruoyi.SSL.controller;

import com.ruoyi.SSL.domain.SslCloudKey;
import com.ruoyi.SSL.domain.SslCloudProductDeploy;
import com.ruoyi.SSL.service.IAutoDeployService;
import com.ruoyi.SSL.service.ISslCloudKeyService;
import com.ruoyi.SSL.service.ISslCloudProductDeployService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/SSL/autoDeployIndex")
public class SslAutoDeployController extends BaseController {

    private String prefix = "SSL/autoDeploy";

    @Autowired
    private IAutoDeployService autoDeployService;

    @Autowired
    private ISslCloudProductDeployService cloudProductDeployService;

    @Autowired
    private ISslCloudKeyService cloudKeyService;

    @GetMapping()
    public String autoDeploy()
    {
        return prefix + "/autoDeployIndex";
    }


    @ResponseBody
    @GetMapping("/getDomainByCsAndKeySercet")
    public AjaxResult getDomainByCsAndKeySercet(String cs, String cloudProduct, String accessKeyId, String accessSecret){
        AjaxResult aj =  autoDeployService.getDomainByCsAndKeySercet(cs, cloudProduct, accessKeyId, accessSecret);
        if(aj.isSuccess()){
            SslCloudKey cloudKey = new SslCloudKey();
            cloudKey.setCloudVendor(cs);
            cloudKey.setAccessKeyId(accessKeyId);
            cloudKey.setAccessKeySecret(accessSecret);
            List<SslCloudKey> cloudKeyList = cloudKeyService.selectSslCloudKeyList(cloudKey);
            if(cloudKeyList == null || cloudKeyList.size() < 1){
                cloudKeyService.insertSslCloudKey(cloudKey);
            }
        }
        return aj;
    }


    @ResponseBody
    @PostMapping("/addCdn")
    public AjaxResult addCdnSave(SslCloudProductDeploy sslCloudProductDeploy)
    {
        return autoDeployService.addCloudProductDeploy(sslCloudProductDeploy);
    }

    @ResponseBody
    @GetMapping("/autoDeploy/{id}")
    public AjaxResult autoDeploy(@PathVariable("id") String id)
    {
        return autoDeployService.deploySsl(id);
    }
}
